<template>
    <link type="text/css" rel="stylesheet" href="/assets/css/style.css">
    <link type="text/css" rel="stylesheet" href="/assets/css/email.css">

    
    <van-cell-group class="input" inset>
        <van-field v-model="business.email" label="邮箱" readonly/>
    </van-cell-group>

    <van-cell-group class="input" inset>
    <van-field
        v-model="code"
        center
        clearable
        label="邮箱验证码"
        placeholder="请输入邮箱验证码"
    >
        <template #button>
        <van-button @click="send" :loading="loading" loading-text="发送中" size="small" type="primary">{{text}}</van-button>
        </template>
    </van-field>
    </van-cell-group>
    
    
    <div class="attest">
        <van-button @click="mail" type="primary">立即认证</van-button>
    </div>
</template>

<script setup>
import { onBeforeMount } from 'vue';

    const {proxy} = getCurrentInstance()

    let business = proxy.$business

    let code = ''

    let loading = ref(false)
    let text = "发送验证码"

    onBeforeMount(()=>{
        waylay()
    })

    const waylay = async () =>
    {
        var result = await proxy.$POST({
                url: 'business/waylay',
                params: {
                    busid: business.value.id,
                }
        })

        if(result.code == 1)
        {
            proxy.$success({
                message: result.msg,
                duration: 1000,
                onClose: () =>
                {
                    proxy.$router.push(result.url)
                    return false
                }
            })
        }
    }

    const send = async () => 
    {
        proxy.$confirm({
             title: '邮件提醒',
             message: '是否确认发送邮件验证码'
        }).then(async ()=>{
            //修改loading状态
            loading.value = true

            var result = await proxy.$POST({
                url: 'business/email',
                params: {
                    busid: business.value.id,
                    email: business.value.email,
                    action: 'send',
                }
            })

            loading.value = false

            if(result.code == 1)
            {
                proxy.$success(result.msg)
                text = '重新发送验证码'
            }else
            {
                proxy.$fail(result.msg)
            }
        }).catch(()=>{})
    }

    const mail = async () =>
    {
        var result = await proxy.$POST({
            url: 'business/mailbox',
            params: {
                busid: business.value.id,
                email: business.value.email,
                code: code,
            }
        })

        if(result.code == 0)
        {
            proxy.$fail(result.msg)
            return false
        }

        //返回上一个界面
        proxy.$success({
            message: result.msg,
            duration: 1000,
            onClose: () => proxy.$router.push(result.url)
        })
    }
</script>